package com.webserver.controller;

import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import com.webserver.vo.Article;
import com.webserver.vo.User;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArticleController {
    private static File articleDir;
    static {
        articleDir = new File("./articles");
        if (!articleDir.exists()) {
            articleDir.mkdirs();
        }
    }
    public void writeArticle(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理文章");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        System.out.println(title+","+author+","+content);

        if (title == null || author == null || content == null ) {
            File file = new File("./webbapps/myweb/article_fail.html");
            response.setEntity(file);
            return;
        }
        File articlefile = new File(articleDir, title + ".obj");
        if (articlefile.exists()){
            File file = new File("./webbapps/myweb/article_fail.html");
            response.setEntity(file);
            return;
        }
        Article article = new Article(title,author,content);
        try(
                FileOutputStream fos = new FileOutputStream(articlefile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                ) {
            oos.writeObject(article);
            File file = new File("./webbapps/myweb/article_success.html");
            response.setEntity(file);

        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("文章处理完毕");

    }
    public void showAllArticle(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始生成动态页面。。。");
        List<Article> articleList = new ArrayList<>();
        File[] subs = articleDir.listFiles(f->f.getName().endsWith(".obj"));
        for(File articleFile : subs){
            try (
                    FileInputStream fis = new FileInputStream(articleFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ){
                Article article = (Article) ois.readObject();
                articleList.add(article);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        articleList.forEach(u-> System.out.println(u));
        try {
            PrintWriter pw = new PrintWriter(
                    "./articleList.html","UTF-8"
            );

            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>文章列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章列表</h1>");
            pw.println("<table border=\"1\">");
            pw.println("<tr>");
            pw.println("<td>文章标题</td>");
            pw.println("<td>文章作者</td>");
            pw.println("<td>文章内容</td>");
            pw.println("</tr>");

            for(Article u : articleList) {
                pw.println("<td>"+u.getTitle()+"</td>");
                pw.println("<td>"+u.getAuthor()+"</td>");
                pw.println("<td>"+u.getContent()+"</td>");
                pw.println("</tr>");
            }
            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
            pw.flush();
            System.out.println("写出完毕");

            File file = new File("./articleList.html");
            response.setEntity(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }




        System.out.println("动态页面生成完毕！");
    }
}
